﻿namespace Resharper.Abstractions.CodeDom.Expressions.LiteralExpressions
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.Tree;
  using Resharper.Abstractions.Actions;
  using Resharper.Abstractions.CodeDom.TreeNodes;

  /// <summary>Defines the <see cref="LiteralExpressionExtensions"/> class.</summary>
  public static class LiteralExpressionExtensions
  {
    #region Public Methods

    /// <summary>Gets the literal expression.</summary>
    /// <param name="treeNode">The tree node.</param>
    /// <returns>Returns the literal expression.</returns>
    [CanBeNull]
    public static LiteralExpression GetLiteralExpression([NotNull] this TreeNode treeNode)
    {
      if (treeNode == null)
      {
        throw new ArgumentNullException("treeNode");
      }

      var literalExpression = treeNode.InternalTreeNode as ILiteralExpression;

      return literalExpression != null ? new LiteralExpression(literalExpression) : null;
    }

    /// <summary>Gets the selected literal expression.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the selected literal expression.</returns>
    [CanBeNull]
    public static LiteralExpression GetSelectedLiteralExpression([NotNull] this ProviderContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var literalExpression = context.InternalGetSelectedElement<ILiteralExpression>();

      return literalExpression != null ? new LiteralExpression(literalExpression) : null;
    }

    #endregion
  }
}